package org.ljy.lotterysystem.common.pojo;

//封装的统一结果返回对象

import lombok.Data;
import org.ljy.lotterysystem.common.errorcode.ErrorCode;
import org.ljy.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import org.springframework.aop.IntroductionInterceptor;
import org.springframework.util.Assert;

@Data
public class CommonResult<T> {

    private Integer code;
    private String msg;
    private T data;

    //1.成功返回
    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<T>();
        result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
        result.data = data;
        result.msg = GlobalErrorCodeConstants.SUCCESS.getMessage();
        return result;
    }

    public static <T> CommonResult<T> success() {
        CommonResult<T> result = new CommonResult<T>();
        result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
        result.msg = GlobalErrorCodeConstants.SUCCESS.getMessage();
        return result;
    }
    //2.返回异常
    public static <T> CommonResult<T> error(Integer code, String msg) {
        //1.判断错误码
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),"code非错误异常码");
        //2.封装结果
        CommonResult<T> result = new CommonResult<T>();
        result.code = code;
        result.msg = msg;
        return result;
    }
    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMessage());
    }

}
